package 
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import hwsoft.webservice_1_0.utils.ParseUtil;

	/**
	 * ...
	 * @author Ning
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
		private var _bg:DisplayObject;
		private var _fg:DisplayObject;
		private var _login:DisplayObject;
		private var _nameView:NameView;

        private var _photoView:PhotoView;
		private var _data:Array;
		private var _fgBox:Sprite;
		private var _user:String;
		//批量载入器
		private var _blukloader:BulkLoader;

		public function Main():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		private function init():void
		{
			AppGlobal.STAGE = this.stage;
			AppGlobal.STAGE.align = StageAlign.TOP_LEFT;
			AppGlobal.STAGE.scaleMode = StageScaleMode.NO_SCALE;
			AppGlobal.REMOTE = new Remote();
			
			AppGlobal.POS_X_ARRAY = [0, 75, 85, 95, 105, 115, 125, 135, 145, 160, 175, 190, 200, 210, 185, 140, 0, -150];
			AppGlobal.POS_Y_ARRAY = [0 , 4 ,  8, 12, 14 ,  18, 22 , 28 , 36 ,  44,  52,  60, 70 , 85 , 100, 120, 140, 140];
			AppGlobal.SCALE_ARRAY = [.2, .22, .24, .26, .28,  .3, .35, .4 ,  .5, .55, .55, .55, .6 , .6 , .65, .7,  1, 1];
			
			AppGlobal.STAGE.addEventListener(Event.RESIZE, onResize);
			
			//载入配置
			var param:Object = this.root.loaderInfo.parameters;
			var url:String = param['config'] == null?'xml/config.xml':param['config'];
			var urlloader:URLLoader = new URLLoader(new URLRequest(url));
			urlloader.addEventListener(Event.COMPLETE, onConfigComplete);
			
			_blukloader = new BulkLoader('blukloader');
		}
		private function onConfigComplete(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, onConfigComplete);
			AppGlobal.CONFIG_XML = XML(evt.target.data);
		//	_user = String(AppGlobal.CONFIG_XML.appSettings.add.(@['key'] == 'user').@['value']);
			
			//载入资源
			_blukloader.add(String(AppGlobal.CONFIG_XML.resource.add.(@['key'] == 'bg').@['value']), {id:'bg' } );
			_blukloader.add(String(AppGlobal.CONFIG_XML.resource.add.(@['key'] == 'fg').@['value']), { id:'fg' } );
		//	_blukloader.add(String(AppGlobal.CONFIG_XML.resource.add.(@['key'] == 'login').@['value']), { id:'login' } );
			_blukloader.addEventListener(BulkProgressEvent.COMPLETE, onResourceComplete);
			_blukloader.start();
		}
		private function onResourceComplete(evt:BulkProgressEvent):void
	    {
			_blukloader.removeEventListener(BulkProgressEvent.COMPLETE, onResourceComplete);
			configUI();
			drawLayout();
			if(ExternalInterface.available)
			    ExternalInterface.call('isReady');
		}
		private function configUI():void
		{
			_bg = _blukloader.getDisplayObjectLoader('bg');
			addChild(_bg);
			_photoView = new PhotoView();
			addChild(_photoView);
			
			AppGlobal.REMOTE.updateData();

			_fgBox = new Sprite();
			addChild(_fgBox);
			_fg = _blukloader.getDisplayObjectLoader('fg');
			_fgBox.addChild(_fg);
			_fgBox.mouseChildren = _fgBox.mouseEnabled = false;
			
		//	_login = _blukloader.getDisplayObjectLoader('login');
		//	_nameView = new NameView();
			
		//	if(_user==null||_user=='')
		//	    addChild(_login);
			//else {
				//addChild(_nameView)
				//_nameView.label = _user;
			//}
		}
		private function drawLayout():void
		{
			_photoView.x = (AppGlobal.STAGE.stageWidth-430)/2+40;
			_photoView.y = 165;
			_bg.x = (AppGlobal.STAGE.stageWidth - 1660) / 2+330;
			_fgBox.x = (AppGlobal.STAGE.stageWidth - 700) / 2 - 135;
		//	_login.x = (AppGlobal.STAGE.stageWidth - 1000) / 2;
		//	_login.y = 0;
		//	_nameView.x = (AppGlobal.STAGE.stageWidth - _nameView.width) / 2+400;
		//	_nameView.y = 80;
		}
		private function onResize(evt:Event):void
		{
			drawLayout();
		}
	}
}